<?php
namespace Admin\Controller;

use Admin\Controller\CommonController;

/**
 * 模块
 * @author wangdong
 */
class CarController extends CommonController
{
    public function index()
    {
        //站点列表
        $this->display();
    }

    public function mgr($draw = 1, $start = 0, $length = 10, $sort = 'id', $order = array(array('dir' => 'desc')))
    {
        if (IS_POST) {
            $dbcar = M('Car');
            $roleid = session('roleid');
            $where = '1=1';
            if($roleid!=1){
                $siteid = session('siteid');
                $where.=' and c.site='.$siteid;

            }
            $total = $dbcar->table("__CAR__ as c")->where($where)->count();
            $list = array();
            if ($total > 0) {

                $order = $sort . ' ' . $order[0]['dir'];
                $limit = $start . "," . $length;
                $list = $dbcar->table("__CAR__ as c")->field("c.*,FROM_UNIXTIME(c.addtime, \"%Y-%m-%d %H:%i:%s\") as addtime,FROM_UNIXTIME(c.online, \"%Y-%m-%d %H:%i:%s\") as onlines,s.status cstatus,si.name sitename")
                    ->JOIN("LEFT JOIN (select car_id,status from __TASK__  where (status=0 or status=1 or status=2)) s on c.id=s.car_id")
                    ->JOIN("LEFT JOIN __SITE__ si on c.site=si.id")
                    ->where($where)
                    ->limit($limit)
                    ->order($order)
                    ->select();
            }
            $data = array(
                "draw" => $draw,
                "recordsTotal" => $total,
                "recordsFiltered" => $total,
                "data" => $list
            );
            $this->ajaxReturn($data);
        } else {
            $this->display();
        }

    }
    /*后台添加用户*/
    public function add()
    {
        $roleid = session('roleid');
        if (IS_POST) {
            $sitedb = M('Car');

            $_validate = array(
                array('mobile', 'require', '手机号码必须填写！'),
                array('name', 'require', '请输入联系人'),
            );
            if (!$sitedb->validate($_validate)->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($sitedb->getError());
            } else {
                $data = I('post.');

                $where = '1=1';
                if($roleid!=1){
                    $siteid = session('siteid');
                    $data['site']=$siteid;
                }
                $p = $data['password'];
                unset($data['password']);
                if (!empty($p)) {
                    $passwords = password($p);
                    $data['password'] = $passwords['password'];
                    $data['encrypt'] = $passwords['encrypt'];
                }

                if (empty($data['id'])) {
                    unset($data['id']);
                    if (empty($data['password'])) {
                        $this->error('密码必须填写');
                    }
                    $data['work_status'] = 1;
                    $data['addtime'] = time();
                    $state = $sitedb->add($data);
                    $state ? $this->success('添加成功') : $this->error('添加失败');
                } else {
                    $id = $data['id'];
                    unset($data['id']);
                    $state = $sitedb->where('id=' . $id)->save($data);
                    $state ? $this->success('修改成功') : $this->error('修改失败');
                }
            }
        } else {
            $id = I('get.id', 0);
            if ($id > 0) {
                $sitedb = M('Car');
                $site = $sitedb->where('id=' . $id)->find();
                $this->assign('info', $site);
            }
            $carType = C('CAR_LOAD');
            $this->assign('carType',explode(',',$carType));
            //获取站点
            if($roleid==1) {
                $site_db = D('Site');
                $sites = $site_db->where('status=0')->select();
                $this->assign('sites', $sites);
            }
            $captain=M('captain')->select();
            $this->assign('captain',$captain);
            $this->assign('roleid', $roleid);
            $this->display();
        }

    }


    public function getOnline()
    {
        $sitedb = M('Car');
        $lng = I('get.lng', 0);
        $lat = I('get.lat', 0);
        $where = 'online>' . (time() - 90);
        if ($lng > 0 && $lat > 0) {
            $carList = $sitedb->field('longitude,latitude,name,id,carno,(ROUND(6378.138*2*ASIN(SQRT(POW(SIN((' . $lat . '*PI()/180-latitude*PI()/180)/2),2)+COS(' . $lat .
                '*PI()/180)*COS(latitude*PI()/180)*POW(SIN((' . $lng . '*PI()/180-longitude*PI()/180)/2),2)))*1000)) AS dis')
                ->table('__SUPPLIER__ as s')->where($where)->order('dis asc')->limit(0, 90)->select();
        } else {
            $carList = $sitedb->field('longitude,latitude,name,id,carno,mobile,load')
                ->where($where)->limit(0, 90)->select();
        }

        $this->ajaxReturn(array('status' => 0, 'carlist' => $carList, 'total' => count($carList)));
    }

    public function online()
    {
//        $where = 'online>' . (time() - 300);
//        $sitedb = M('Car');
//        $carList = $sitedb->field('longitude,latitude,name,id,carno')
//            ->where($where)->select();
//        $this->assign('carlist', $carList);
        $this->display();
    }
}